package com.encryption.service.ca.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.log4j.Logger;

import com.encryption.dao.CertBindDAO;
import com.encryption.dao.CertDetailDAO;
import com.encryption.model.CertBind;
import com.encryption.model.CertDetail;
import com.encryption.model.User;
import com.encryption.service.ca.CAService;
import com.encryption.service.ca.CertManageService;
import com.encryption.service.ca.CertQueryFacade;
import com.encryption.service.ca.identify.model.CAServiceResult;
import com.encryption.service.ca.util.CAConstants;
import com.encryption.service.ca.util.PropertyLoader;
import com.encryption.service.ca.util.StringUtil;
import com.sun.org.apache.xml.internal.security.utils.Base64;

public class CertQueryFacadeImpl implements CertQueryFacade {
    private static final Logger logger        = Logger.getLogger(CertQueryFacadeImpl.class);
    
    private CertBindDAO certBindDAO = new CertBindDAO();
    private CertDetailDAO      certDetailDAO = new CertDetailDAO();
    
    private CAService          caService     = new CAServiceImpl();
    
    private CertManageService   manageService = new CertManageServiceImpl(); 
    
    
    public static final String CSR_CMD     = "cmd=gencsr&csp_name=Microsoft Enhanced Cryptographic Provider v1.0";
    
    public Boolean isApplyCert(String userId) {
        List bindList = certBindDAO.findByUserId(userId);
        if (bindList == null || bindList.size() == 0)
            return false;
        CertBind certBind = (CertBind) bindList.get(0);
        if(StringUtil.equalsIgnoreCase("1",certBind.getCertStatus())){
            return true;
        }
        return false;
    }

    public String getCSRCmd() {
        return CSR_CMD;
    }

    public String getInstallCmd(User user, String csr) {
        StringBuffer sb = new StringBuffer();
        sb.append("cmd=installcert&pkcs7pkg=");
        CAServiceResult result = null;
        try {
            result = caService.generatePKCS7(user.getId() + "", user.getRealName(), csr, "");
            File root_file = new File(user.getRealName() + ".cer");
            FileOutputStream fos = null;
            if (!root_file.exists()) {
                fos = new FileOutputStream(root_file);
                
                fos.write(Base64.decode(result.getCertB64String()));
                fos.close();
            } 
        } catch (Exception e) {
            e.printStackTrace();
        } 
        manageService.bindCert(user.getId() + "", "", result.getCertSn(), result
            .getUserCertThrumbId(), "");
        
        sb.append(result.getP7pkg());
        
        return sb.toString();
    }

    public String getLogonCmd(String userId,String random) {
        List<CertDetail> certDetailList = (List<CertDetail>) certDetailDAO.findByUserId(userId);
        StringBuffer sb = new StringBuffer();
        sb.append("cmd=logon&random=");
        sb.append(random);
        sb.append("&certsns=");
        for (CertDetail certDetail : certDetailList) {
            sb.append(certDetail.getCertSn());
            sb.append("X");
        }
        return StringUtil.chop(sb.toString());
        
    }
    
}

